<template>
  <div style="background-color: #f6f5f6; padding-bottom: 50px">
    <van-nav-bar left-text="返回" left-arrow @click-left="$router.back()">
      <template #title>
        {{ community }}
      </template>
    </van-nav-bar>
    <!-- ---------- -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="(item, index) in houseImg" :key="index">
        <img :src="item" alt="加载失败" />
      </van-swipe-item>
    </van-swipe>
    <!-- ----------- -->
    <div class="details">
      <div class="title">
        <p>{{ title }}</p>
        <span class="tags" v-for="(item, index) in tags" :key="index">{{
          item
        }}</span>
      </div>
      <div class="bod">
        <van-grid :border="false" :column-num="3">
          <van-grid-item>
            <span class="price">
              {{ price }}
              <span class="unit">/月</span>
            </span>
            <span class="intr">租金</span>
          </van-grid-item>
          <van-grid-item>
            <span class="price">
              {{ roomType }}
            </span>
            <span class="intr">房型</span>
          </van-grid-item>
          <van-grid-item>
            <span class="price"> {{ size }}平米 </span>
            <span class="intr">面积</span>
          </van-grid-item>
        </van-grid>
      </div>
      <div class="foot">
        <div class="sub">
          <p>
            <span class="intr">装修：</span>
            精装
          </p>
          <p>
            <span class="intr">楼层：</span>
            {{ floor }}
          </p>
        </div>
        <div class="sub">
          <p>
            <span class="intr">朝向：</span>
            <span v-for="(item, index) in oriented" :key="index">
              {{ item }}
            </span>
          </p>
          <p>
            <span class="intr">类型：</span>
            普通住宅
          </p>
        </div>
      </div>
    </div>
    <!-- ------------ -->
    <div class="address">
      <div class="xiaoqu">
        <p>小区：天山星城</p>
      </div>
    </div>
    <!-- ------------ -->
    <div class="supporting">
      <div class="titles">房屋配套</div>
      <div>
        <van-grid :border="false" :column-num="5">
          <van-grid-item
            v-for="(item, index) in supporting"
            :key="index"
            :text="item"
          >
            <template #icon>
              <van-icon :name="icon[item]" />
            </template>
          </van-grid-item>
          <div>
            <van-grid-item v-if="supporting.length === 0">
              <span style="font-size: 14px; border: none">没有更多配置了</span>
            </van-grid-item>
          </div>
        </van-grid>
      </div>
    </div>
    <!-- ------------- -->
    <div class="overview supporting">
      <div class="titles">房源概况</div>
      <div class="user">
        <div class="left body">
          <img src="http://liufusong.top:8080/img/avatar.png" alt="头像" />
          <div class="name">
            <span>王女士</span>
            <span class="vip">
              <van-icon name="shield-o" />
              已认证房主
            </span>
          </div>
        </div>
        <div class="right body">
          <div class="btn">发消息</div>
        </div>
      </div>
      <div>{{ description }}</div>
    </div>
    <!-- --------------- -->
    <div class="footer">
      <div @click='add(isFavorite)'>
        <span v-if='!isFavorite'><van-icon name="star-o" /></span>
        <span v-else class='active'><van-icon name="star" /></span>
          <span>收藏</span>
      </div>
      <div>
        <span>在线咨询</span>
      </div>
      <div class='ment'>
        电话预约
      </div>
    </div>
  </div>
</template>

<script>
import { houseId, getFavorites, addFavorites, delFavorites } from '@/api/article'
export default {
  async created() {
    const code = this.$route.query.code
    // console.log(code)
    const {body:{isFavorite}} = await getFavorites(code)
    // console.log(isFavorite)
    const {
      body: {
        title,
        community,
        floor,
        oriented,
        tags,
        houseImg,
        price,
        roomType,
        size,
        supporting,
        description,
        houseCode
      }
    } = await houseId(code)
    this.title = title
    this.community = community
    this.floor = floor
    this.oriented = oriented
    this.houseImg = houseImg.map((i) => 'http://liufusong.top:8080' + i)
    this.tags = tags
    this.price = price
    this.roomType = roomType
    this.size = size
    this.supporting = supporting
    this.description = description
    this.houseCode = houseCode
    this.isFavorite = isFavorite
  },
  data() {
    return {
      isFavorite: false,
      houseCode: '',
      description: '',
      community: '',
      floor: '',
      oriented: [],
      houseImg: [],
      price: '',
      roomType: '',
      size: '',
      supporting: [],
      tags: [],
      title: '',
      icon: {
        天然气: 'fire-o',
        衣柜: 'send-gift-o',
        暖气: 'hot-o',
        洗衣机: 'send-gift',
        空调: 'idcard',
        冰箱: 'column',
        电视: 'tv-o',
        热水器: 'bullhorn-o',
        宽带: 'bar-chart-o',
        沙发: 'desktop-o'
      }
    }
  },
  methods: {
    async add(val) {
      // console.log(val);
      if (val) {
        await delFavorites(this.houseCode)
        // console.log('删除',res);
        this.isFavorite = false
        this.$toast.fail('已取消收藏')
      } else {
        await addFavorites(this.houseCode)
        // console.log('添加',re);
        this.isFavorite = true
        this.$toast.success('收藏成功')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  height: 252px;
  line-height: 252px;
  text-align: center;
}
.active {
  color: #ff4f00;
}
.details {
  padding: 0 10px;
  margin-bottom: 10px;
  background-color: #fff;
  overflow: hidden;
  .title {
    height: 70px;
    border-bottom: 1px solid #cecece;
    .tags {
      display: inline-block;
      padding: 4px 5px;
      margin-right: 5px;
      font-size: 12px;
      border-radius: 3px;
      line-height: 12px;
      color: #39becd;
      background-color: #e1f5f8;
    }
  }
  .bod {
    border-bottom: 1px solid #cecece;
    .price {
      color: #fa5741;
      font-size: 18px;
      font-weight: bolder;
      .unit {
        font-size: 12px;
        font-weight: 400;
      }
    }
    .intr {
      font-size: 14px;
      color: #999;
    }
  }
  .foot {
    display: flex;
    height: 5rem;
    .sub {
      width: 50%;
      height: 100%;
      font-size: 14px;
      .intr {
        color: #999;
      }
    }
  }
}
.address {
  height: 190px;
  background-color: #fff;
  overflow: hidden;
  .xiaoqu {
    padding: 0 10px;
  }
}
.supporting {
  padding: 10px;
  overflow: hidden;
  background-color: #fff;
}
.titles {
  padding-bottom: 10px;
  border-bottom: 1px solid #cecece;
}
.overview {
  margin: 10px 0;
  // height: 265px;
  background-color: #fff;
  .user {
    display: flex;
    margin-bottom: 20px;
    height: 62px;
    .body {
      width: 50%;
    }
    .left {
      display: flex;
      img {
        margin: 10px 10px 0 0;
        width: 52px;
        height: 52px;
      }
      .name {
        margin-top: 20px;
        display: flex;
        flex-direction: column;
        font-size: 14px;
        .vip {
          font-size: 12px;
          color: #fa5741;
        }
      }
    }
    .right {
      position: relative;
      .btn {
        position: absolute;
        right: 15px;
        top: 20px;
        padding: 3px 15px;
        color: #33be85;
        border: 1px solid #33be85;
        border-radius: 3px;
      }
    }
  }
}
.footer {
  position: fixed;
  bottom: 0;
  display: flex;
  width: 100%;
  height: 3.125rem;
  background-color: #fff;
  div {
    flex: 1;
    text-align: center;
    line-height: 3.125rem;
    border-top: 1px solid #cecece;
    border-right: 1px solid #cecece;
  }
  .ment {
    border-right: 0;
    color: #fff;
    background-color: #33be85;
  }
}
</style>
